package com.songlanyun.modules.freebag.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.disabilityauth.service.DisabilityAuthService;
import com.songlanyun.modules.freebag.dao.FreeBagDao;
import com.songlanyun.modules.freebag.entity.FreeBag;
import com.songlanyun.modules.freebag.service.FreeBagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service("freeBagService")
public class FreeBagServiceImpl extends ServiceImpl<FreeBagDao, FreeBag> implements FreeBagService {

    @Resource
    private DisabilityAuthService disabilityAuthService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FreeBag> page = this.page(
                new Query<FreeBag>().getPage(params),
                new LambdaQueryWrapper<FreeBag>()
                        .like(ObjectUtil.isNotNull(params.get("free_name")), FreeBag::getFreeName, params.get("free_name"))
                        .orderByDesc(FreeBag::getUpdateTime));
        return new PageUtils(page);
    }

    @Override
    public FreeBag loadById(Integer id, boolean hasThrow) {
        FreeBag freeBag = this.baseMapper.selectById(id);
        if (ObjectUtil.isNull(freeBag) && hasThrow) {
            throw new RRException("未找到该福利包信息");
        }
        return freeBag;
    }

    @Override
    public FreeBag add(FreeBag dto) {
        FreeBag load = this.loadByName(dto.getFreeName());
        if (ObjectUtil.isNotNull(load)) {
            throw new RRException("福利包名称不能重复");
        }
        //看同类型，同次数，发放规则，发放次数的是否已经存在(不允许)
        load = this.loadBy(dto);
        if (ObjectUtil.isNotNull(load)) {
            throw new RRException("该类型和类型次数，发放规则和发放次数已经存在");
        }
        this.baseMapper.insert(dto);
        return dto;
    }

    @Override
    public FreeBag update(FreeBag dto) {
        FreeBag current = this.loadById(dto.getId(), true);
        FreeBag load = this.loadByName(dto.getFreeName());
        if (ObjectUtil.isNotNull(load) && !load.getId().equals(current.getId())) {
            throw new RRException("福利包名字不能重复");
        }
        load = this.loadBy(dto);
        //看同类型，同次数，发放规则，发放次数的是否已经存在(不允许)
        if (ObjectUtil.isNotNull(load) && !load.getId().equals(current.getId())) {
            throw new RRException("该类型和类型次数，发放规则和发放次数已经存在");
        }
        this.baseMapper.updateById(dto);
        return dto;
    }

    @Override
    public void deleteById(Integer id) {
        //查询该福利包是否被使用
        this.verifyFreeBag(id);
        //删除
        baseMapper.deleteById(id);
    }

    @Override
    public void verifyFreeBag(Integer id) {
        Integer count = disabilityAuthService.loadByFreeBagId(id);
        if (count > 0) {
            throw new RRException("该福利包已被关联");
        }
    }

    private FreeBag loadBy(FreeBag dto) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<FreeBag>()
                .eq(FreeBag::getFreeType, dto.getFreeType())
                .eq(FreeBag::getFreeNum, dto.getFreeNum())
                .eq(FreeBag::getIntegralType, dto.getIntegralType())
                .eq(FreeBag::getIntegralNum, dto.getIntegralNum())
                .last("limit 1"));
    }

    private FreeBag loadByName(String name) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<FreeBag>()
                .eq(FreeBag::getFreeName, name).last("limit 1"));
    }
}
